-- UIGainAirshipSnapshot
-- Create by chengb Dec/20/2016
-- 获得飞艇屏幕快照

-- UIGainAirshipSnapshot继承自Layer
UIGainAirshipSnapshot = class("UIGainAirshipSnapshot", function()
    return cc.Layer:create();
end);

function UIGainAirshipSnapshot.create(shipId)
    return UIGainAirshipSnapshot.new(shipId);
end

-- 构造函数
function UIGainAirshipSnapshot:ctor(shipId)
    -- 初始化
    self:setName("UIGainAirshipSnapshot");
    local node = cc.CSLoader:createNode("layout/snapshot/GainPetSnapshot.csb");
    self:addChild(node);
    self.node = node;

    self.shipId = shipId;

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIGainAirshipSnapshot:resize()
    local node = self.node;
    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;

    -- bg居中显示并充满整个屏幕
    AlignM.alignToCenter(node, "bg");

    local heroBg = findChildByName(node, "bg/hero_bg");
    if h / w < 1.5 then
        heroBg:setScaleX(768 / DESIGN_WIDTH);
    end

    -- 二维码图片缩放
    local scale = math.min(w / DESIGN_WIDTH, h / DESIGN_HEIGHT);
    local codeImg = findChildByName(node, "qr_code");
    codeImg:setScale(scale);
    codeImg:setPosition(w / 2, 40 * scale);

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- TODO 限制界面大小不超过参考尺寸
    local curSize = math.max(w, h);
    if curSize > SHARE_IMAGE_REF_SIZE then
        local scale = SHARE_IMAGE_REF_SIZE / curSize;
        node:setScale(scale);
    end
end

-- 获取界面的缩放比例
function UIGainAirshipSnapshot:getNodeScale()
    return self.node:getScale();
end

-- 重绘
function UIGainAirshipSnapshot:redraw()
    local CT = self.node:getChildByName("CT");

    -- 标题
    local titleImg = findChildByName(CT, "title_icon");
    titleImg:loadTexture("images/logo/cn_game_name.png");
    titleImg:setScale(1.1);

    -- 显示飞艇名字
    local nameLabel = findChildByName(CT, "name");
    local shipName = NewAirShipM.query(self.shipId, "name");
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_LARGE2, TextStyleM.TEXT_COLOR_ORANGE, true, -2, nil, nil, nil, nil, true);
    nameLabel:setString(shipName);


    -- 显示飞艇对话
    local descLabel  = findChildByName(CT, "desc");
    if not isOverseaPackage() then
        -- 国内平台不显示描述，需要显示二维码
        descLabel:setVisible(false);
    else
        descLabel:setVisible(true);
        local desc = NewAirShipM.query(self.shipId, "talk");
        TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BRIGHT_YELLOW);
        descLabel:setString(desc);
    end

    -- 创建冈布奥模型
    self:createShipModel();

    -- 重绘招募部分信息
    self:redrawPetRecruitNode();

    -- 显示二维码
    self:redrawQRCode();
end

-- 重绘招募部分信息
function UIGainAirshipSnapshot:redrawPetRecruitNode()
    -- 获取阵营信息
    local campId = NewAirShipM.query(self.shipId, "camp");

    -- 背景
    local heroBg = findChildByName(self.node, "bg/hero_bg");
    local campIconPath = getHeroBgIconPath("gain_ship_bg");
    heroBg:loadTexture(campIconPath);

    -- 效果
    local effectImg = findChildByName(self.node, "bg/effect_img");
    effectImg:loadTexture(getMercenaryCampIconPath("effect" .. campId));
    effectImg:stopAllActions();
    effectImg:removeAllChildren();
    local effectList = {1287, 1286, 1288, 1289};
    playEffect(effectImg, effectList[campId], 300, 250, nil, 2.35);
end

-- 创建飞艇模型
function UIGainAirshipSnapshot:createShipModel()
    local headImg = findChildByName(self.node, "CT/hero_icon");
    local icon = NewAirShipM.query(self.shipId, "icon");
    local shipIconPath = getAirShipImgPath(icon);
    headImg:loadTexture(shipIconPath);
    headImg:setVisible(true);
end

-- 显示二维码
function UIGainAirshipSnapshot:redrawQRCode()
    local QRCodeImg = findChildByName(self.node, "qr_code");

    -- 国内平台，显示二维码
    if not isOverseaPackage() then
        QRCodeImg:setVisible(true);
        QRCodeImg:loadTexture("images/logo/lt_qr_code.png");
    else
        QRCodeImg:setVisible(false);
    end
end

